package com.zyd.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.zyd.module.admin.mapper.ProductTypeMapper;
import com.zyd.module.admin.model.ProductType;
import com.zyd.module.admin.service.ProductTypeService;

import java.util.Arrays;
import java.util.List;


/**
 * @author tiezhu
 * @date 2022年4月6日
 */
@Service("productTypeService")
public class ProductTypeServiceImpl extends ServiceImpl<ProductTypeMapper, ProductType> implements ProductTypeService {

    @Autowired
    private ProductTypeMapper productTypeMapper;

    @Override
    public List<ProductType> selectTypeEntityList(ProductType typeEntity) {
        return productTypeMapper.selectTypeEntityList(typeEntity);
    }

    @Override
    public List<ProductType> selectByFid(Integer fid) {
        QueryWrapper<ProductType> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("fid",fid);
        return productTypeMapper.selectList(queryWrapper);
    }

    @Override
    public List<ProductType> selectList() {
        QueryWrapper<ProductType> queryWrapper=new QueryWrapper<>();
        queryWrapper.ne("fid",0);
        return productTypeMapper.selectList(null);
    }

    @Override
    public List<ProductType> selectTypeNodes() {
        List<ProductType> typeList = productTypeMapper.selectList(null);
        QueryWrapper<ProductType> queryWrapper=new QueryWrapper<>();
        for (ProductType type:typeList) {
            if (type.getFid()!=0){
                queryWrapper.eq("id",type.getFid());
                ProductType parent = productTypeMapper.selectOne(queryWrapper);
                type.setParent(parent);
            }
            queryWrapper.clear();
            if (type.getId()!=null){
                queryWrapper.eq("fid",type.getId());
                List<ProductType> list = productTypeMapper.selectList(queryWrapper);
                type.setChildren(list);
            }

        }
        return typeList;
    }

    @Override
    public int deleteBatch(Integer[] ids) {
        return productTypeMapper.deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    public ProductType selectById(Integer id) {
        return productTypeMapper.selectById(id);
    }
}